<?php
declare(strict_types=1);
/**
 * MineAdmin is committed to providing solutions for quickly building web applications
 * Please view the LICENSE file that was distributed with this source code,
 * For the full copyright and license information.
 * Thank you very much for using MineAdmin.
 *
 * @Author X.Mo<root@imoi.cn>
 * @Link   https://gitee.com/xmo/MineAdmin
 */

namespace App\Admin\Mapper;

use App\Exception\AdminException;
use App\Model\Car;
use App\Model\CarCategory;
use App\Model\Order;
use Hyperf\Collection\Arr;
use Hyperf\Collection\Collection;
use Hyperf\Database\Model\Builder;
use Mine\Abstracts\AbstractMapper;
use Mine\MineModel;

/**
 * 车辆列表Mapper类
 */
class CarMapper extends AbstractMapper
{
    /**
     * @var Car
     */
    public $model;

    public function assignModel()
    {
        $this->model = Car::class;
    }

    public function read(mixed $id, array $column = ['*']): ?MineModel
    {
        $model = parent::read($id, $column);
        if (is_null($model)) {
            return $model;
        }

        // 分类查询
        $categories = new Collection();
        if (!empty($model->car_category_ids)) {
            $categories = CarCategory::query()
                ->whereIn('id', array_unique($model->car_category_ids))
                ->pluck('name', 'id')
                ->filter();
        }

        $model->car_category_texts = $this->getCategoryText($model, $categories);
        return $model;
    }

    public function delete(array $ids): bool
    {
        // 查询员工是否关联订单
        $count = Order::query()
            ->whereIn('car_id', $ids)
            ->count();
        if ($count > 0) {
            throw new AdminException('存在关联订单无法删除');
        }
        return parent::delete($ids);
    }

    /**
     * 搜索处理器
     * @param Builder $query
     * @param array $params
     * @return Builder
     */
    public function handleSearch(Builder $query, array $params): Builder
    {
        
        // 名称
        if ( isset($params['name']) && filled($params['name']) ) {
            $query->where('name', 'like', '%'.$params['name'].'%');
        }

        return $query;
    }

    public function handlePageItems($items)
    {
        // 分类查询
        $categoryIds = Arr::flatten(pluck($items, 'car_category_ids'));
        $categories = new Collection();
        if (!empty($categoryIds)) {
            $categories = CarCategory::query()
                ->whereIn('id', array_unique($categoryIds))
                ->pluck('name', 'id')
                ->filter();
        }

        /** @var Car $model */
        foreach ($items as $model) {
            $model->car_category_texts = $this->getCategoryText($model, $categories);
        }
        return $items;
    }

    /**
     * 获取分类文字
     * @param Car $model
     * @param Collection $categories
     * @return array
     */
    private function getCategoryText(Car $model, Collection $categories): array
    {
        $res = [];
        foreach ($model->car_category_ids as $carCategoryId) {
            if (!empty($categories[$carCategoryId])) {
                $res[] = $categories[$carCategoryId];
            }
        }
        return $res;
    }
}